#!/usr/bin/env bash

set -e

cd "$(dirname "$0")/.."

if [ -f "Brewfile" ] && [ "$(uname -s)" = "Darwin" ] && [ "$SKIP_BREW" != "1" ] && [ -t 0 ]; then
  brew bundle check >/dev/null 2>&1 || {
    echo -n "==> Install Homebrew dependencies? (y/N): "
    read -r response
    case "$response" in
      [yY][eE][sS]|[yY])
        brew bundle
        ;;
      *)
        ;;
    esac
    echo
  }
fi

echo "==> Installing Node dependencies…"

PACKAGE_MANAGER=$(command -v yarn >/dev/null 2>&1 && echo "yarn" || echo "npm")

$PACKAGE_MANAGER install "$@"

./scripts/build

echo
echo "==> Installing vertex-sdk dependencies"
(cd packages/vertex-sdk && $PACKAGE_MANAGER install)

echo
echo "==> Installing bedrock-sdk dependencies"
(cd packages/bedrock-sdk && $PACKAGE_MANAGER install)

echo
echo "==> Installing foundry-sdk dependencies"
(cd packages/foundry-sdk && $PACKAGE_MANAGER install)